package org.zt.java.dal.config;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

import org.zt.java.dal.config.vo.ApplicationInfo;
import org.zt.java.dal.config.vo.DataSourceConfig;
import org.zt.java.dal.config.vo.MethodRouterConfig;
import org.zt.java.dal.util.DalConfigUtil;



public class DalConfig {
	private ApplicationInfo applicationInfo;
	private DataSourceConfig dataSourceConfig;
	//支持精确匹配，类名，方法名都是全的
	private Map</* class_method_param */String, MethodRouterConfig> methodRouteConfigMap = new HashMap<>();
	// 支持模糊匹配，类名是全的，方法名是不全的，方法名格式为：find*，save*等。放长度排序，长度越长的，排在越靠前
	private Map</* class_methodPrefix */String, MethodRouterConfig> fuzzyMethodRouteConfigMap = new TreeMap<>(
			new Comparator<String>() {
				@Override
				public int compare(String key1, String key2) {
					return key2.compareTo(key1);
				}
			});

	public DalConfig() {
		super();
	}

	public ApplicationInfo getApplicationInfo() {
		return applicationInfo;
	}

	public void setApplicationInfo(ApplicationInfo applicationInfo) {
		this.applicationInfo = applicationInfo;
	}

	public DataSourceConfig getDataSourceConfig() {
		return dataSourceConfig;
	}

	public void setDataSourceConfig(DataSourceConfig dataSourceConfig) {
		this.dataSourceConfig = dataSourceConfig;
	}

	public Map<String, MethodRouterConfig> getMethodRouteConfigMap() {
		return methodRouteConfigMap;
	}

	public void setMethodRouteConfigMap(Map<String, MethodRouterConfig> methodRouteConfigMap) {
		this.methodRouteConfigMap = methodRouteConfigMap;
	}

	public Map<String, MethodRouterConfig> getFuzzyMethodRouteConfigMap() {
		return fuzzyMethodRouteConfigMap;
	}

	public void setFuzzyMethodRouteConfigMap(Map<String, MethodRouterConfig> fuzzyMethodRouteConfigMap) {
		this.fuzzyMethodRouteConfigMap = fuzzyMethodRouteConfigMap;
	}
	
	public void addMethodRouteConfig(MethodRouterConfig methodRouterConfig) {
		methodRouteConfigMap.put(buildMethodRouteIndexKey(methodRouterConfig),
				methodRouterConfig);
	}
	
	public void addFuzzyMethodRouteConfig(MethodRouterConfig methodRouterConfig) {
		fuzzyMethodRouteConfigMap.put(DalConfigUtil.getFuzzyMethodRouteIndexKey(methodRouterConfig.getClassName(),
				methodRouterConfig.getMethod()), methodRouterConfig);
	}
	
	private String buildMethodRouteIndexKey(
			MethodRouterConfig methodRouterConfig) {
		return DalConfigUtil.getMethodRouteIndexKey(methodRouterConfig.getClassName(),
				methodRouterConfig.getMethod(), methodRouterConfig.getParam());
	}
}
